import os
# from subprocess import Popen, PIPE
#
#
# cmd_list = [r"D:\Program Files\Notepad++\notepad++.exe", "adb connect 127.0.0.1:7555"]
# p = Popen(cmd_list[0], shell=True, stdout=PIPE, stderr=PIPE)
# p.wait()
# res = p.stdout.read().decode("utf-8")
# print(res)
# if "10061" in res:
#     print(f"设备连接失败！！！\nError: {res}")

# return <os._wrap_close object at 0x000001CD6A8669D0>
# a = os.popen("adb devices")
# print(a)
#
# text = a.read()
# print(text)
# a.close()
# a = os.popen('adb connect 127.0.0.1:7555')
# print(a.read())

# with os.popen(r'D:\Program Files\MuMu\emulator\nemu\EmulatorShell\NemuPlayer.exe', 'r') as f:
#     text = f.read()
#     print(text)
#     f.close()
    # if 'connected' in text.split(" "):
    #     print(text)

# with os.popen(r'adb connect 127.0.0.1:7555', 'r') as f:
#     text = f.read()
#     # print(text)
#     if 'connected' in text.split(" "):
#         print(text)

import os
import subprocess
from time import sleep

cmd = r"D:\Program Files\MuMu\emulator\nemu\EmulatorShell\NemuPlayer.exe"
subprocess.Popen(cmd)

def connect():
    with os.popen(r'adb devices', 'r') as f:
        text = f.read()
    s = text.split("\n")
    result = [x for x in s if x != '']
    devices = []
    for i in result:
        dev = i.split('\tdevice')
        if len(dev) >= 2:
            devices.append(dev[0])
    if not devices:
        return False
    else:
        print(f"当前已连接设备： {devices}")
        return True
#
# for i in range(20):
#     subprocess.Popen(r'adb connect 127.0.0.1:7555')
#     if connect():
#         break
#     sleep(2)

# import subprocess
# def runcmd(command):
#     ret = subprocess.run(command,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,encoding="utf-8",timeout=20)
#     if ret.returncode == 0:
#         print("success:",ret)
#     else:
#         print("error:",ret)
# runcmd(cmd)

import os

os.system("appium")